//
//  StatusCell.swift
//  SinaWeibo
//
//  Created by liyue on 15/11/14.
//  Copyright © 2015年 李越. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin: CGFloat = 12
let StatusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {

    var bottomConstraints: Constraint?
    
    var status: Status? {
        
        didSet {
            originalView.status = status
            
            self.bottomConstraints?.uninstall()
            
            if let re = status?.retweeted_status {
                retweetedView.retweetedStatus = re
                retweetedView.hidden = false
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.top.equalTo(retweetedView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
            else {
                
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.top.equalTo(originalView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(bottomView)
        
        
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
//            make.height.equalTo(50)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentView.snp_left)
            self.bottomConstraints = make.top.equalTo(retweetedView.snp_bottom).offset(StatusCellMargin).constraint
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
    }
    
    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()

    private lazy var retweetedView: ReweetedStatusView = ReweetedStatusView()
}
